# -*- coding: utf-8 -*-

from decimal import Decimal

import numpy as np
import pandas as pd


def pandas_decimal():
    df1 = pd.DataFrame(data=[['111', 2.3, 3], ['333', 3.5, 5], ['444', 4.7, 7]],
                       index=[1, 2, 3], columns=['A', 'B', 'C'])
    s1 = df1['B'].astype(str).apply(Decimal)

    df2 = df1[['B', 'C']].astype(str).applymap(Decimal)


def df_op():
    df1 = pd.DataFrame(data=[['111', 2.3, 3], ['333', 3.5, 5], ['444', 4.7, 7]],
                       index=[1, 2, 3], columns=['A', 'B', 'C'])
    df1.set_index('A', inplace=True)

    df2 = pd.DataFrame(data=[['111', 2.3, 3], ['222', 3.5, 5], ['666', 4.7, 7]],
                       index=[1, 2, 3], columns=['A', 'B', 'C'])
    df2.set_index('A', inplace=True)

    df3 = df1.drop(index=set(df2.index.tolist()).intersection(set(df1.index.tolist())), axis=0)
    df4 = pd.concat([df2, df3 * 0])
    df4.sub(df1, fill_value=0.0)
    df3 = df1.sub(df2)


def pd_cut():
    # 示例数据
    data = {'score': [5, 15, 25, 45, 59, 65, 75]}
    df = pd.DataFrame(data)

    # 定义分段的边界（左开右闭区间，注意边界设计）
    bins = [df['score'].min(), 18, 35, 59, df['score'].max()]
    labels = ['未成年', '青年', '中年', '老年']

    df['level'] = '未分类'
    df['level'] = np.where(df['score'] == 18, '未成年', df['level'])
    df['level'] = np.where(df['score'] < 18, '未成年', df['level'])
    df['level'] = np.where((df['score'] >= 18) & (df['score'] < 35), '青年', df['level'])
    # 使用 pd.cut
    # df['level'] = pd.cut(df['score'], bins=bins, labels=labels, right=False)

    print(df)


if __name__ == '__main__':
    # pandas_decimal()
    pd_cut()
